<?	/*
	// File:	linux-domain.wcp.phps
	// Purpose:	linux domain manipulation
	// Author:	Felix <webmaster@can-host.com>
	*/

// Handle domains manipulation
if ($type == "domain") {
	
	// Handle Domain Creation :: expect *
	if ($data['action'] == 'create') {
		
		// Create new account
		if ($data['type'] == 'domain' OR $data['type'] == 'subdomain') {
			// Alias ID to domain if its a sub, else create a new dir; create group & set hd quota if necessary.
			if ($data['type'] == 'subdomain') {
				$gid = "g".$data['owner'];
				symlink($cfg['webdir']."/".$data['owner'], $data['path']);
			}
			elseif ($data['type'] == 'domain') {
				$gid = "g".$data['id'];
				$hdquota = $data['hd'] * 1024;
				if ($data['hd'] == -1) { $hdquota = 0; }
				if ($cfg['os'] == 'linux' || $cfg['os'] == '') {
					exec($cfg['prog']['gadd']." ".$gid);
					switch ($cfg['osversion']) {
						case 'RedHat6.2':
							$setq = $cfg['prog']['squota']." -g $gid ".$cfg['devhd']." $hdquota $hdquota 0 0";
							break;
						default:
							$setq = $cfg['prog']['squota']." -g $gid $hdquota $hdquota 0 0 ".$cfg['devhd'];
					}
					exec($setq);
				}
				elseif ($cfg['os'] == 'freebsd') {
					exec($cfg['prog']['pw']." groupadd -q -n $gid");
					/*
						To add a group quota on FreeBSD:
						edquota -e /home:0:99999999 -g group
						adds a soft/hard limit of 0/9999999 kb for the group 'group' on filesystem /home
					*/
					exec($cfg['prog']['squota']." ".$cfg['webdir'].":0:$hdquota -g $gid");
				}

                                // Check if this is a subdomain account.
                                if($data['type']=="domain"){
                                    $dbp = mysql_query("SELECT domain FROM domains WHERE domain='".$data['domain']."' and type='domain' and action = ''");
                                    if(mysql_num_rows($dbp)) $data['sub']=true;
                                }

				if (!file_exists($data['path']))
					mkdir($data['path'],0755);
					chgrp($data['path'],$gid);
				
				if (!file_exists($data['path']."/_logs"))
					mkdir($data['path']."/_logs",0755);
					chgrp($data['path']."/_logs",$gid);
					
				if (!file_exists($data['path']."/_backups")) {
					mkdir($data['path']."/_backups",0777);
					chgrp($data['path']."/_backups",$gid);
					chown($data['path']."/_backups",$cfg['httpd_user']);
				}
				
				if (!file_exists($data['path']."/cgi-bin")) {
					mkdir($data['path']."/cgi-bin",0777);
					chgrp($data['path']."/cgi-bin",$gid);
					chown($data['path']."/cgi-bin",$cfg['httpd_user']);
				}
			}
			
			if (file_exists($data['path']) AND $gid) {
				// Create needed directories
				$path = $data['path']."/".$data['host'];
				if (!file_exists($path))
					mkdir($path,0755);
				if (!file_exists($path."/".$cfg['webname']))
					mkdir($path."/".$cfg['webname'],0775);
				if (!file_exists($path."/users"))
					mkdir($path."/users",0775);
				if (!file_exists($path."/data"))
					mkdir($path."/data",0775);
				if (!file_exists($path."/ssl"))
					mkdir($path."/ssl",0755);
				if (!file_exists($path."/logs"))
					mkdir($path."/logs",0755);
				
				// Copy over skel content to new account, then 'parse' index.html
				if (!file_exists($path."/".$cfg['webname']."/index.html"))
					exec($cfg['prog']['cp']." -Rf ".$cfg['basedir']."/skel/* ".$path."/".$cfg['webname']);
				
				if (file_exists($path."/".$cfg['webname']."/index.html")) {
					// fetch reseller name & email
					$tmp = fetchdata("name,email","reseller",$data['owner']);
					$index_skel = @file_get_contents($path."/".$cfg['webname']."/index.html");
					$index_skel = str_replace("[HOST]",$data['host'],$index_skel);
	 				$index_skel = str_replace("[DOMAIN]",$data['domain'],$index_skel);
	 				$index_skel = str_replace("[RESELLER NAME]",$tmp['name'],$index_skel);
	 				$index_skel = str_replace("[RESELLER EMAIL]",$tmp['email'],$index_skel);
	 				// write it back parsed
	 				file_put_contents($path."/".$cfg['webname'].'/index.html',$index_skel);
	 			}
				
				// Set group permissions
				chgrp($path,$gid);
	 			chgrp($path."/".$cfg['webname'],$gid);
	 			chgrp($path."/users",$gid);
	 			chgrp($path."/data",$gid);
	 			chgrp($path."/ssl",$gid);
	 			chgrp($path."/logs",$gid);
	 			
	 			// Set final permissions on the dir structure.
	 			chmod($path,0755);
				chmod($path."/".$cfg['webname'],0775);
				chmod($path."/users",0755);
				chmod($path."/data",0775);
				chmod($path."/ssl",0755);
				chmod($path."/logs",0755);
	 			
	 			// Set permissions on all new web files.
	 			$webdir = dir_list($path."/".$cfg['webname'],"file");
	 			while (list($key, $val) = each($webdir)) {
	 				chmod($val,0664);
	 				chgrp($val,$gid);
	 			}
	 			
	 			// Set permissions on all new web dirs.
	 			$webdir = dir_list($path."/".$cfg['webname'],"dir");
	 			while (list($key, $val) = each($webdir)) {
	 				chmod($val,0775);
	 				chgrp($val,$gid);
	 			}
	 			
	 			// Create domain symbolic link (look friendlier to customers)
	 			if ((!$data['sub'] && !@is_link($cfg['webdir']."/".$data['domain'])) OR ($data['sub'] && !@is_link($cfg['webdir']."/".$data['host'].".".$data['domain'])) AND $data['type'] != "pointer"){
					if(!$data['sub']) symlink($data['path'], $cfg['webdir']."/".$data['domain']);
                                        else  symlink($data['path'], $cfg['webdir']."/".$data['host'].".".$data['domain']);
				}
				
				// Add cgi-bin if perl support is needed
				if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
					if (!file_exists($path."/".$cfg['webname']."/cgi-bin")) {
						mkdir($path."/".$cfg['webname']."/cgi-bin", 0775);
						chmod($path."/".$cfg['webname']."/cgi-bin",0775);
						chgrp($path."/".$cfg['webname']."/cgi-bin",$gid);
					}
				}
			}
			else // else log it: error
				webcp_log(0,0,"system","Domain Creation Error: ".$data['path']." does not exist",0);
		}
		
		// Create only cgi-bin for pointer if its needed.
		elseif ($data['type'] == "pointer") {
			// get group id
			$gid = "g".$data['owner'];
			
			// Add cgi-bin if perl support is needed
			if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
				if (!file_exists($path."/cgi-bin")) {
					mkdir($path."/cgi-bin", 0775);
					chmod($path."/cgi-bin",0775);
					chgrp($path."/cgi-bin",$gid);
				}
			}
		}
	}
	
	// Handle Domain updates :: expect *
	elseif ($data['action'] == 'update') {
					
		// use host1 for the path in case of a hostname change
		$path = $data['path'];
		
		// get group id and set quota if needed
		if ($data['type'] == 'subdomain' OR $data['type'] == 'pointer')
			$gid = "g".$data['owner'];
		
		elseif ($data['type'] == 'domain') {
			$gid = "g".$data['id'];
			$hdquota = $data['hd'] * 1024;
			if ($data['hd'] == -1) { $hdquota = 0; }
			if ($cfg['os'] == 'linux') {
				switch ($cfg['osversion']) {
					case 'RedHat6.2':
						exec($cfg['prog']['squota']." -g $gid ".$cfg['devhd']." $hdquota $hdquota 0 0");
						break;
					default:
						exec($cfg['prog']['squota']." -g $gid $hdquota $hdquota 0 0 ".$cfg['devhd']);
				}
			}
			elseif ($cfg['os']== 'freebsd') {
				exec($cfg['prog']['squota']." ".$cfg['webdir'].":0:$hdquota -g $gid");
			}
		}
		
		// Add cgi-bin if perl support is needed
		if ($data['type'] == 'pointer') {
			if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
				if (!file_exists($path."/cgi-bin") AND file_exists($path)) {
					mkdir($path."/cgi-bin", 0775);
					chmod($path."/cgi-bin",0775);
					chgrp($path."/cgi-bin",$gid);
				}
			}
		}
		else {
			if (strstr($cfg['sslang'],"perl") AND $data['serverside']['perl'] == 'on') {
				if (!file_exists($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin")) {
					mkdir($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin", 0775);
					chmod($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin",0775);
					chgrp($path."/".$data['host1']."/".$cfg['webname']."/cgi-bin",$gid);
				}
			}
		}
		
		// Check if host or domain changed, handle it.
		if ($data['host'] != $data['host1'] AND $data['type'] != "pointer") {
			$oldpath = $data['path']."/".$data['host1'];
			$newpath = $data['path']."/".$data['host'];
			rename($oldpath,$newpath);
			mysql_query("UPDATE domains SET host1=host WHERE id='".$data['id']."'");
		}
		if ($data['domain'] != $data['domain1'] AND $data['type'] != "pointer") {
			$oldpath = $cfg['webdir']."/".$data['domain1'];
			$newpath = $cfg['webdir']."/".$data['domain'];
			rename($oldpath,$newpath);
			mysql_query("UPDATE domains SET domain1=domain WHERE id='".$data['id']."'");
		}
		// update domain pointers
		if (($data['host'] != $data['host1'] OR $data['domain'] != $data['domain1']) AND $data['type'] == "pointer")
			mysql_query("UPDATE domains SET domain1=domain,host1=host WHERE id='".$data['id']."'");
	}
	
	// Suspend Domain :: expect 'id'
	elseif ($data['action'] == "suspend") {
		if ($data['id']) {
			$tmp = fetchdata("type,path","domain",$data['id']);
			
			if ($tmp['type'] == "domain") {
							// The httpd conf file has the document root modified for suspended domain
				// suspend users
				$dbp = mysql_query("SELECT username,type FROM users WHERE id='".$data['id']."'");
				while ($udata = mysql_fetch_array($dbp)) {
					$udata['action'] = 'suspend';
					webcp("user",$udata);
				}
			
				// suspend sub-domains & domain pointers
				$dbp = mysql_query("SELECT id, type, path FROM domains WHERE owner='".$data['id']."'");
				while ($ddata = mysql_fetch_array($dbp)) {
					$ddata['action'] = 'suspend';
					webcp("domain",$ddata);
				}
				
				// chmod home dir 0 if its a domain
				chmod($tmp['path'],0);
			}
		}
	}
	
	// Unsuspend Domain :: expect 'id',
	elseif ($data['action'] == "unsuspend") {
		if ($data['id']) {
			$tmp = fetchdata("type,path","domain",$data['id']);			
			
			if ($tmp['type'] == "domain") {
			
				// The httpd conf file has the document root reset, unsuspend users
				$SQL = "SELECT username, type FROM users WHERE id='".$data['id']."'";
				$dbp = mysql_query($SQL);
				while ($udata = mysql_fetch_array($dbp)) {
					$udata['action'] = 'unsuspend';
					webcp("user",$udata);
				}
			
				// unsuspend sub-domains & domain pointers
				$dbp = mysql_query("SELECT id, type, path FROM domains WHERE owner='".$data['id']."'");
				while ($ddata = mysql_fetch_array($dbp)) {
					$ddata['action'] = 'unsuspend';
					webcp("domain",$ddata);
				}
				
				// chmod home dir 0755 if its a domain
				chmod($tmp['path'],0755);
			}
				
		}
	}
	
	// Remove Domain :: expect *
	elseif ($data['action'] == "remove") {
		if ($data['id'] AND file_exists($data['path'])) {
			// If its a domain, remove all subdomains and domain pointers and its users
			if ($data['type'] == "domain") {
				$dbp = mysql_query("SELECT id, type, path, hostname, sub, domain FROM domains WHERE owner='".$data['id']."' AND (type = 'subdomain' OR type='pointer')");
				while ($ddata = mysql_fetch_array($dbp)) {
					$ddata['action'] = 'remove';
					webcp("domain",$ddata);
				}
				$dbp = mysql_query("SELECT users.username, users.id, users.type, domains.domain FROM users INNER JOIN domains ON users.id = domains.id WHERE users.id='".$data['id']."'");
				while ($udata = mysql_fetch_array($dbp)) {
					$udata['action'] = 'remove';
					webcp("user",$udata);
				}
			}
			
			// If its a domain remove WEBDIR/ID
			if ($data['type'] == 'domain') {
				// Remove all files.
	 			$webdir = dir_list($data['path'],"file");
	 			while (list($key, $val) = each($webdir)) {
	 				if (!is_dir($val)) {
		 				unlink($val);
		 			}
		 		}
				// Remove all sub dirs.
	 			$webdir = dir_list($data['path'],"dir");
	 			arsort($webdir);
	 			reset($webdir);
	 			while (list($key, $val) = each($webdir))
	 				rmdir($val);
	 			// Remove WEBDIR/ID
	 			@rmdir($data['path']);
	 			// Unlink domain symbolic link
                                if($data['sub'] !== false) unlink($cfg['webdir']."/".$data['domain']);
                                else unlink($cfg['webdir']."/".$data['hostname'].".".$data['domain']);
	 			
	 			// Delete group
	 			$gid = "g".$data['id'];
	 			if ($cfg['os'] == 'linux')
	 				exec($cfg['prog']['gdel']." $gid");
	 			elseif ($cfg['os'] == 'freebsd')
	 				exec($cfg['prog']['pw']." groupdel -q -n $gid");
	 		}
	 		// If its a sub-domain remove WEBDIR/ID/subname
			elseif ($data['type'] == 'subdomain') {
				$path = $data['path']."/".$data['host1'];
				// Remove all files.
	 			$webdir = dir_list($path,"file");
	 			while (list($key, $val) = each($webdir))
	 				unlink($val);
				// Remove all dirs
	 			$webdir = dir_list($path,"dir");
	 			arsort($webdir);
	 			reset($webdir);
	 			while (list($key, $val) = each($webdir))
	 				rmdir($val);
	 			// Remove WEBDIR/ID
	 			@rmdir($path);
	 			// unlink ID alias
	 			unlink($data['path']);
	 		}
	 	}
	}
}
?>
